<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>Docker基础 - 记录从零开始的过程</title><meta name="Description" content="记录从零开始的过程"><meta property="og:title" content="Docker基础" />
<meta property="og:description" content="介绍docker的基础用法" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://example.org/docker%E5%9F%BA%E7%A1%80/" /><meta property="og:image" content="http://example.org/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2021-11-10T17:24:47+08:00" />
<meta property="article:modified_time" content="2021-11-10T17:24:47+08:00" />

<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="http://example.org/logo.png"/>

<meta name="twitter:title" content="Docker基础"/>
<meta name="twitter:description" content="介绍docker的基础用法"/>
<meta name="application-name" content="LoveIt">
<meta name="apple-mobile-web-app-title" content="LoveIt"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="http://example.org/docker%E5%9F%BA%E7%A1%80/" /><link rel="prev" href="http://example.org/%E5%AE%89%E8%A3%85docker/" /><link rel="next" href="http://example.org/docker%E5%AE%89%E8%A3%85nginx/" /><link rel="stylesheet" href="/lib/normalize/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "Docker基础",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "http:\/\/example.org\/docker%E5%9F%BA%E7%A1%80\/"
        },"genre": "posts","keywords": "docker","wordcount":  1365 ,
        "url": "http:\/\/example.org\/docker%E5%9F%BA%E7%A1%80\/","datePublished": "2021-11-10T17:24:47+08:00","dateModified": "2021-11-10T17:24:47+08:00","publisher": {
            "@type": "Organization",
            "name": ""},"author": {
                "@type": "Person",
                "name": "Cuasloi"
            },"description": ""
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="记录从零开始的过程">Cuasloi</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="记录从零开始的过程">Cuasloi</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">Docker基础</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>Cuasloi</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/docker/"><i class="far fa-folder fa-fw"></i>docker</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2021-11-10">2021-11-10</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 1365 字&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 3 分钟&nbsp;</div>
        </div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#docker">Docker</a>
      <ul>
        <li><a href="#docker是什么">Docker是什么</a></li>
        <li><a href="#两个重要的概念">两个重要的概念</a></li>
        <li><a href="#dockerhub">DockerHub</a></li>
        <li><a href="#docker的基本操作">Docker的基本操作</a>
          <ul>
            <li><a href="#镜像">镜像</a></li>
            <li><a href="#容器">容器</a></li>
            <li><a href="#数据卷">数据卷</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><p>介绍docker的基础用法</p>
<h2 id="docker">Docker</h2>
<p>关于Docker的概念去<a href="https://docs.docker.com/get-started/overview/" target="_blank" rel="noopener noreffer">官网</a>查看，这里只是快速入门使用。</p>
<h3 id="docker是什么">Docker是什么</h3>
<p>我把Docker当成一个小型虚拟机，与Hyper-V等虚拟机不同的是Hyper-V等虚拟机可以使用操作系统的镜像文件安装操作系统，
而Docker可以使用应用程序的镜像文件<strong>运行</strong>应用程序。</p>
<h3 id="两个重要的概念">两个重要的概念</h3>
<p><strong>镜像</strong>：Docker将应用程序及其所需要的依赖，环境，配置等打包在一起称为镜像。
类似操作系统的ISO镜像文件，这里叫image.</p>
<p><strong>容器</strong>：将镜像文件加载到内存中隔离运行，形成单一进程。类似Hyper-V中的虚拟机，Hyper-V可以创建多个虚拟机安装同一个镜像(<code>iso</code>)，
而Docker可以创建多个容器运行同一个镜像(<code>image</code>)。</p>
<h3 id="dockerhub">DockerHub</h3>
<p><a href="https://hub.docker.com/" target="_blank" rel="noopener noreffer">DockerHub</a>是下载Docker镜像的地方，一个官方的镜像仓库。</p>
<h3 id="docker的基本操作">Docker的基本操作</h3>
<h4 id="镜像">镜像</h4>
<ol>
<li>
<p>基础命令</p>
<p><strong><code>--help</code></strong> 可以查看命令帮助</p>
<ul>
<li><code>docker search</code> 搜索镜像</li>
<li><code>docker pull</code> 下载镜像</li>
<li><code>docker images</code> 查看本地镜像</li>
<li><code>docker rmi</code> 删除镜像</li>
<li><code>docker push</code> 上传镜像</li>
<li><code>docker save</code> 保存镜像为一个tar包</li>
<li><code>docker load</code> 加载tar包成一个镜像</li>
</ul>
</li>
<li>
<p>命令介绍</p>
<p><code>docker search ngi</code> #搜索nginx
<figure><a class="lightgallery" href="/images/docker/d1.png" title="/images/docker/d1.png" data-thumbnail="/images/docker/d1.png" data-sub-html="<h2>搜索nginx</h2>">
        <img
            class="lazyload"
            src="/svg/loading.min.svg"
            data-src="/images/docker/d1.png"
            data-srcset="/images/docker/d1.png, /images/docker/d1.png 1.5x, /images/docker/d1.png 2x"
            data-sizes="auto"
            alt="/images/docker/d1.png" width="80%" />
    </a><figcaption class="image-caption"><code>搜索nginx</code></figcaption>
    </figure>
<code>docker pull nginx[:latest]</code> #下载nginx镜像，不指定版本默认版本使用latest,表示最新版本
<figure><a class="lightgallery" href="/images/docker/d2.png" title="/images/docker/d2.png" data-thumbnail="/images/docker/d2.png" data-sub-html="<h2>下载nginx镜像</h2>">
        <img
            class="lazyload"
            src="/svg/loading.min.svg"
            data-src="/images/docker/d2.png"
            data-srcset="/images/docker/d2.png, /images/docker/d2.png 1.5x, /images/docker/d2.png 2x"
            data-sizes="auto"
            alt="/images/docker/d2.png" width="80%" />
    </a><figcaption class="image-caption"><code>下载nginx镜像</code></figcaption>
    </figure>
<code>docker images</code> #查看已下载镜像
<figure><a class="lightgallery" href="/images/docker/d3.png" title="/images/docker/d3.png" data-thumbnail="/images/docker/d3.png" data-sub-html="<h2>查看已下载镜像</h2>">
        <img
            class="lazyload"
            src="/svg/loading.min.svg"
            data-src="/images/docker/d3.png"
            data-srcset="/images/docker/d3.png, /images/docker/d3.png 1.5x, /images/docker/d3.png 2x"
            data-sizes="auto"
            alt="/images/docker/d3.png" width="80%" />
    </a><figcaption class="image-caption"><code>查看已下载镜像</code></figcaption>
    </figure>
<code>docker save -o nginx.tar nginx[:latest]</code> #将nginx镜像打包成 nginx.tar 文件</p>
<p><code>docker rmi nginx:latest</code> #删除镜像</p>
<p><code>docker load -i nginx.tar</code> #将nginx.tar文件加载为镜像</p>
</li>
</ol>
<h4 id="容器">容器</h4>
<ol>
<li>
<p>基础命令</p>
<ul>
<li><code>docker run</code> 创建并运行一个容器</li>
<li><code>docker pause</code> 让一个运行的容器暂停</li>
<li><code>docker unpause</code> 从暂停状态恢复运行</li>
<li><code>docker stop</code> 停止运行</li>
<li><code>docker start</code> 重新运行一个容器，只有停止的容器可以重新运行</li>
<li><code>docker rm</code> 删除一个容器</li>
<li><code>docker exec</code> 进入容器执行命令</li>
<li><code>docker logs</code> 查看容器运行日志</li>
<li><code>docker ps</code> 查看所有运行的容器及状态</li>
<li><code>docker cp</code> 容器与主机之间的数据复制</li>
</ul>
</li>
<li>
<p>命令介绍</p>
<p>具体使用镜像可以到<a href="https://hub.docker.com/" target="_blank" rel="noopener noreffer">DockerHub</a>搜索查看镜像使用文档</p>
<p><code>docker run --name myNginx -p 80:80 -d nginx</code></p>
<ul>
<li>
<p><strong>&ndash;name</strong> 指定容易的名称</p>
</li>
<li>
<p><strong>-p</strong> 将容器暴露的端口映射到Linux系统的端口，冒号右侧是容器暴露的端口。</p>
</li>
<li>
<p><strong>-d</strong> 后台运行容器</p>
</li>
<li>
<p><strong>nginx</strong> 镜像名称</p>
</li>
</ul>
<p>容器是一个隔离环境，访问Linux系统的80端口访问不到容器中的nginx，所以需要做端口映射。
现在访问linux系统80端口可以看到nginx已经正常运行了。</p>
<p><code>docker ps -a</code> # 查看容器  <strong>-a</strong> 显示停止的容器</p>
<p><code>docker logs myNginx</code> # 查看myNginx容器日志 <strong>-f</strong> 可以动态查看日志</p>
<p><code>docker exec -it myNginx bash</code></p>
<ul>
<li>
<p><strong>-it</strong> 给当前进入的容器创建一个标准输入、输出终端</p>
</li>
<li>
<p><strong>myNginx</strong> 容器的名称</p>
</li>
<li>
<p><strong>bash</strong> 进入后执行的命令，bash是一个终端交互命令行工具</p>
</li>
</ul>
<p>容器内部是一个独立的Linux文件系统， 它包含了nginx应用程序所需要的环境、配置等。
容器内并不是全部Linux系统，很多Linux命令这里无法使用。</p>
</li>
</ol>
<h4 id="数据卷">数据卷</h4>
<p>数据卷是一个虚拟目录，映射到Linux主机文件系统中的某个目录(<code>/var/lib/docker/volume</code>)。</p>
<ol>
<li>
<p>基础命令</p>
<ul>
<li><code>docker volume create test</code> 创建一个<strong>test</strong>数据卷</li>
<li><code>docker volume inspect test</code> 显示<strong>test</strong>数据卷的信息</li>
<li><code>docker volume ls</code> 显示所有数据卷</li>
<li><code>docker volume prune</code> 删除未使用的数据卷</li>
<li><code>docker volume test</code> 删除<strong>test</strong>数据卷</li>
</ul>
</li>
<li>
<p>数据卷挂载</p>
<ul>
<li>在创建容器时，使用 -v 参数挂载数据卷到容器中的某个目录。</li>
<li><code>docker run --name myNginx -p 80:80 -v html:/usr/share/nginx/html -d nginx</code> 
#将<strong>html</strong>数据卷挂载到容器<code>/usr/share/nginx/html</code>目录，如果数据卷不存在会自动创建。</li>
</ul>
</li>
<li>
<p>目录和文件挂载</p>
<p>与数据卷挂载语法一致，把数据卷名称替换成主机目录的绝对路径即可，
这种挂载方式会覆盖容器中的目录及内容，始终与主机目录保持一致。</p>
</li>
</ol></div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2021-11-10</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="http://example.org/docker%E5%9F%BA%E7%A1%80/" data-title="Docker基础"><i class="fab fa-weibo fa-fw"></i></a></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="/tags/docker/">docker</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/%E5%AE%89%E8%A3%85docker/" class="prev" rel="prev" title="安装Docker"><i class="fas fa-angle-left fa-fw"></i>安装Docker</a>
            <a href="/docker%E5%AE%89%E8%A3%85nginx/" class="next" rel="next" title="Docker安装nginx">Docker安装nginx<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
<div id="comments"></div></article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.89.0">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">Cuasloi</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><script type="text/javascript" src="/lib/smooth-scroll/smooth-scroll.min.js"></script><script type="text/javascript" src="/lib/autocomplete/autocomplete.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.stemmer.support.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.zh.min.js"></script><script type="text/javascript" src="/lib/lazysizes/lazysizes.min.js"></script><script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script><script type="text/javascript" src="/lib/sharer/sharer.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":10},"comment":{},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"lunr"}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
